(function (window, $)
{
    var common = {};
    common.Defaults = {};

    common.Defaults.Dialog = {
        title : "提示",
        message : "",
        buttons : []
    };
    //弹出对话框
    common.showDialog = function (options)
    {
        options = $.extend({}, common.Defaults.Dialog, options);
        var html = [];
        html.push("<div class=\"modal fade\" tabindex=\"-1\" role=\"dialog\" aria-labelledby=\"myModalLabel\" aria-hidden=\"true\" style=\"display: none;\">");
        html.push("    <div class=\"modal-dialog\">");
        html.push("        <div class=\"modal-content\">");
        html.push("            <div class=\"modal-header\">");
        html.push("                <button type=\"button\" class=\"close\" data-dismiss=\"modal\" aria-hidden=\"true\">×</button>");
        html.push("                <h4 class=\"modal-title\" id=\"myModalLabel\"></h4>");
        html.push("            </div>");
        html.push("            <div class=\"modal-body\">");
        html.push("            </div>");
        html.push("            <div class=\"modal-footer\">");
        html.push("            </div>");
        html.push("        </div>");
        html.push("    </div>");
        html.push("</div>");

        var g = $(html.join(""));
        $("body").append(g);

        g.title = $("h4.modal-title", g);
        g.content = $("div.modal-body", g);
        g.foot = $("div.modal-footer", g);

        if (options.title)
        {
            g.title.html(options.title);
        }

        if (options.message)
        {
            g.content.html(options.message);
        }

        if (options.buttons.length > 0)
        {
            $(options.buttons).each(function (i, item)
            {
                var btn = $("<button type=\"button\" class=\"btn " + (item.cls ? item.cls : "btn-default") + "\">" + (item.text) + "</button>");
                if (item.click)
                {
                    btn.bind("click", function ()
                    {
                        item.click(g);
                    });
                }
                g.foot.prepend(btn);
            });
        }

        g.modal("show");
        return g;
    };

    common.info = function (message)
    {
        var g = this;
        var options = {
            title : "提示",
            message : message,
            buttons : [
                {
                    cls : "btn-default",
                    text : "确认",
                    click : function (dialog)
                    {
                        dialog.modal('hide');
                    }
                }
            ]
        };

        g.showDialog(options);
    };

    common.confirm = function (message, func)
    {
        var args = [];
        for (var i = 2, len = arguments.length; i < len; ++i)
        {
            args.push(arguments[i]);
        }
        var g = this;
        var options = {
            title : "提示",
            message : message,
            buttons : [
                {
                    cls : "btn-default",
                    text : "取消",
                    click : function (dialog)
                    {
                        dialog.modal('hide');
                    }
                },
                {
                    cls : "btn-primary",
                    text : "确认",
                    click : function (dialog)
                    {
                        func.apply(this, args);
                        dialog.modal('hide');
                    }
                }
            ]
        };

        g.showDialog(options);
    };

    common.format = function (value, dateformat)
    {
        var date = new Date(parseInt(value));
        var g = this, p = this.options;
        if (isNaN(date))
        {
            return null;
        }
        var format = dateformat;
        var o = {
            "M+" : date.getMonth() + 1,
            "d+" : date.getDate(),
            "h+" : date.getHours(),
            "m+" : date.getMinutes(),
            "s+" : date.getSeconds(),
            "q+" : Math.floor((date.getMonth() + 3) / 3),
            "S" : date.getMilliseconds()
        };
        if (/(y+)/.test(format))
        {
            format = format.replace(RegExp.$1, (date.getFullYear() + "")
                .substr(4 - RegExp.$1.length));
        }
        for (var k in o)
        {
            if (new RegExp("(" + k + ")").test(format))
            {
                format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k]
                    : ("00" + o[k]).substr(("" + o[k]).length));
            }
        }
        return format;
    };

    window.common = common;

    $('body').on('hidden.bs.modal', '.modal', function ()
    {
        $(this).remove();
    });

})(window, jQuery);